/*
Copyright 2008  Four Roads LLC conact@4-roads.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either 
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CommunityServer.Components;
using CommunityServer.Controls;

namespace FourRoads.Links.Controls
{
	/// <summary>
	/// Summary description for ResLinkCommentInsert.
	/// </summary>
	public class ResLinkCommentInsert : ResLinkBaseTemplatedWebControl
	{
		#region Child Controls
		Button _AddBtn;
		Button _CancelBtn;
		ResLinkCommentRateLink	_Rate;
		#endregion

		public ResLinkCommentInsert()
		{

		}

		public override void DataBind()
		{
			base.DataBind();

			if (ThisSection.EnableRatings == false)
			{
				_Rate.Visible = false;
			}
			
			_Rate.SectionID = ThisSection.SectionID;
			_Rate.PostID = CSContext.Current.GetIntFromQueryString("PostID" , -1);

			Context.Items["CommunityServer.Controls.Title.Value"] =  ResLinkLink.GetSingleLink(_Rate.PostID).Subject;
		}


		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad (e);

			if(!Page.IsPostBack)
			{
				DataBind();
			}
		}

		protected override void AttachChildControls()
		{
			_AddBtn = FindControl("AddComment") as Button;
			_CancelBtn= FindControl("CancelAddComment") as Button;
			_Rate = FindControl("Rate") as ResLinkCommentRateLink;

			_Rate.RatingsChanged += new EventHandler(_Rate_RatingsChanged);

			_AddBtn.Click += new EventHandler(_AddBtn_Click);
			_CancelBtn.Click += new EventHandler(_CancelBtn_Click);

			_CancelBtn.CausesValidation = false;
		}

		private void _AddBtn_Click(object sender, EventArgs e)
		{
			//So we have got to the point where we can post the comment
			if (Page.IsValid)
			{
				ResLinkComments comment = new ResLinkComments();

				TextBox commentText = FindControl("CommentEdt") as TextBox;
				comment.CommentRating = -1;

				if (ThisSection.EnableRatings)
				{	
					comment.CommentRating = _Rate.NewRating;	
				}

				comment.Body = commentText.Text.Replace("\r\n" , "<br />");
				comment.FormattedBody = comment.Body;
				comment.User = CSContext.Current.User;
				comment = ResLinkLink.AddComment(ThisSection , comment  , PostID);

				if (comment.IsApproved == false)
				{
					Modal.ClosePage(Page , "'-1:"+comment.PostID + "'");
				}
				else
				{
					Modal.ClosePage(Page ,"'"+PostID.ToString()+":"+comment.PostID+ "'");
				}
			}
		}

		public int PostID
		{
			get
			{
				return CSContext.Current.GetIntFromQueryString("PostID" , -1);
			}
		}

		private void _CancelBtn_Click(object sender, EventArgs e)
		{
			Modal.ClosePage(Page , "'0:0'");
		}

		private void _Rate_RatingsChanged(object sender, EventArgs e)
		{
			DataBind();
		}
	}
}
